/* Check operation of ADC button peripheral in board

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include "esp_log.h"
#include "board.h"
#include "esp_peripherals.h"
#include "periph_adc_button.h"

#include "audio_element.h"
#include "audio_pipeline.h"
#include "audio_event_iface.h"
#include "audio_error.h"
#include "tone_stream.h"
#include "i2s_stream.h"
#include "mp3_decoder.h"

#include "audio_tone_uri.h"
#include "yu_adf_def.h"

static const char *TAG = "yu_adf_main";

void yu_play_tone_task(void *pre)
{
    esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
    esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
    audio_board_key_init(set);
    audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
    audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
    audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);
    while (1)
    {
        audio_event_iface_msg_t msg;
        esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
        if (ret != ESP_OK)
        {
            continue;
        }
        if ((msg.source_type == PERIPH_ID_TOUCH || msg.source_type == PERIPH_ID_BUTTON || msg.source_type == PERIPH_ID_ADC_BTN) && (msg.cmd == PERIPH_ADC_BUTTON_PRESSED))
        {
            if ((int)msg.data == get_input_play_id())
            {
                play_tone(TONE_TYPE_ALARM);
            }
            else if ((int)msg.data == get_input_rec_id())
            {
                play_tone(TONE_TYPE_WIFI_SUCCESS);
            }
            else if ((int)msg.data == get_input_volup_id())
            {
                play_tone(TONE_TYPE_SHUTDOWN);
            }
            else if ((int)msg.data == get_input_voldown_id())
            {
                play_tone(TONE_TYPE_UPGRADE_DONE);
            }
            else if ((int)msg.data == get_input_set_id())
            {
                play_tone(TONE_TYPE_FREETALK);
            }
            else if ((int)msg.data == get_input_mode_id())
            {
                play_tone(TONE_TYPE_WECHAT);
            }
        }
    }
}

void app_main(void)
{
    play_tone_init();
    play_tone(TONE_TYPE_WELCOME_TO_WIFI);
    xTaskCreate(yu_play_tone_task, "yu_play_tone_task", 4096, NULL, 5, NULL);
    xTaskCreate(lcd_lvgl_task, "lvgl_task", 4096, NULL, 5, NULL);
    while (1)
    {
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}
